<?php
/**
 * User: ngreduce
 * Date: 11.03.11
 * Time: 19:06
 */

Yii::import('ext.feed.*');
 
class FeedController extends Controller {
    public $itemsInFeed = 20;
    private $_feed;

    public static $feeds = array(
        'jobs',
        'resume',
    );

    public function actionJobs()
    {
        $this->_initFeed(Yii::t('mj-base', 'Jobs'), $this->createAbsoluteUrl('job/index'));

        $criteria=new CDbCriteria(array(
			'condition'=>'status='.Job::STATUS_APPROVED,
			'order'=>'update_time DESC',
			'with'=>'requestCount',
            'limit'=>$this->itemsInFeed,
		));
        $jobs = Job::model()->findAll($criteria);

        foreach ($jobs as $job) {
            $url = $this->createAbsoluteUrl('job/view', array('id'=>$job->id, 'title'=>$job->title));
            $item = $this->_feed->createNewItem();
            
            $item->link = $url;
            $item->title = $job->title;
            $item->date = $job->create_time;
            $item->description = mb_substr($job->content, 0, 150, 'UTF-8').'...';
            $item->addTag('author', ($job->author != null) ? $job->author->username : '');
            $item->addTag('guid', $url ,array('isPermaLink'=>'true'));
            $this->_feed->addItem($item);
        }
        $this->_feed->generateFeed();
    }

    public function actionResume()
    {
        $this->_initFeed(Yii::t('mj-base', 'Resume'), $this->createAbsoluteUrl('resume/index'));

        $criteria=new CDbCriteria(array(
			'condition'=>'status='.Job::STATUS_APPROVED,
			'order'=>'update_time DESC',
			'with'=>'requestCount',
            'limit'=>$this->itemsInFeed,
		));
        $resumeModel = Job::model()->findAll($criteria);

        foreach ($resumeModel as $resume) {
            $url = $this->createAbsoluteUrl('resume/view', array('id'=>$resume->id, 'title'=>$resume->title));
            $item = $this->_feed->createNewItem();

            $item->link = $url;
            $item->title = $resume->title;
            $item->date = $resume->create_time;
            $item->description = mb_substr($resume->content, 0, 150, 'UTF-8').'...';
            $item->addTag('author', ($resume->author != null) ? $resume->author->username : '');
            $item->addTag('guid', $url ,array('isPermaLink'=>'true'));
            $this->_feed->addItem($item);
        }
        $this->_feed->generateFeed();
    }

    protected function _initFeed($title, $url)
    {
        $this->_feed = new EFeed;

        $this->_feed->link = $url;
        $this->_feed->addChannelTag('language', Yii::app()->language);
        $this->_feed->addChannelTag('pubDate', date(DATE_RSS, time()));
        $this->_feed->title = Yii::app()->name.' - '.$title;
    }
}
